dojo.provide("dojoscratch.Scratch");
dojo.require("dojox.gfx");
dojo.require("dojo.colors");
dojo.require("dojox.gfx.Moveable");

dojo.require("dojoscratch.Scalable");

dojo.declare("dojoscratch.Scratch", null, {
	constructor: function(surface, id, shape, fill, stroke, matrix) {
		// Create shape in group
		this._group = surface.createGroup();
		this._shape = this._group.createShape(shape);
		
		// fill color
		if (fill) {
			this._shape.setFill(fill.colors.color);
		}
		// set stroke
		if (stroke) {
			console.debug(stroke);
			this._shape.setStroke(stroke);
		}
		if (matrix) {
			console.debug(matrix);
			this.transform(matrix);
		}
		this._scratchId = id;
		console.debug("constructing scratch with id"  + id);
		
		this._connectToEvents();

		// set movable and scalable state
		this.setMovable(false);
		this.setScalable(false);
		
		this._init();
	},
	remove: function() {
		this._group.removeShape(true);
	},
	//by convention 0 - send to back, -1 - bring to front
	changeZIndex: function(zIndex) {
		if (0 == zIndex) {
			this._group.moveToBack();
		} else if (-1 == zIndex) {
			this._group.moveToFront();
		} else {
			console.debug("unknown z index: "  + zIndex);
		}
	},
	_init: function() {
		//nothing to do... to be overrided
	},
	_connectToEvents : function() {
		//send transformations to server
		var domShape = this._group.getEventSource();
		dojo.connect(domShape, "onmouseup", this, "_handleMouseUp");
		dojo.connect(domShape, "onmousedown", this, "_handleMouseDown");
	},
	transform: function(matrix) {
		this._group.setTransform(matrix);
	},
	_handleMouseDown: function(event) {
		dojo.publish("activated", [this]);
	},
	_handleMouseUp: function(event) {
		dojo.publish("activated", [this]);
		dojo.publish("scratchTransform", [this._scratchId, this._group.getTransform()]);
	},
	setScalable: function(enable) {
		if(true == enable) {
			if(!this._scaleHandler) {
				this._scaleHandler = new dojoscratch.Scalable(this._group);
				var domShape = this._group.getEventSource();
//				this._mouseUpConnection = dojo.connect(domShape, "onmouseup", this, "_handleMouseUp");
				
				//disable move
				this.setMovable(false);
			}
		} else {
			if(this._scaleHandler) {
				this._scaleHandler.destroy();
//				dojo.disconnect(this._mouseUpConnection);
				this._scaleHandler = null;
				this.setMovable(true);
			}
		}
	},
	setMovable: function(enable) {
		if(true == enable) {
			if(!this._moveHandler) {
				this._moveHandler = new dojox.gfx.Moveable(this._group);
			}
		} else {
			if(this._moveHandler) {
				this._moveHandler.destroy();
				this._moveHandler = null;
			}
		}
	},
	onDeactivation: function() {
		if (this._marker) {
			this._marker.removeShape(true);
		}
	},
	getBoundingBox: function() {
		return this._shape.getBoundingBox(); 
	},
	getTransformedBoundingBox: function() {
		var box = this._shape.getTransformedBoundingBox();
		if (box) {
			return {x: box[0].x, y:box[0].y, width: box[2].x-box[0].x, height: box[2].y-box[0].y}; 
		} else {
			return this.getBoundingBox();
		}
	},
	onActivation: function() {
		var box = this.getBoundingBox();
		
		this._marker= this._group.createRect({
			"type" : "rect",
			"x" : 	box.x-5,
			"y" : box.y-5,
			"width" : box.width+10,
			"height" : box.height+10,
			"r" : 0
		})
        .setStroke({
        	//one from: http://docs.dojocampus.org/dojox/gfx-visual-properties
				style : "DashDot"
		});
	}
});